package com.purong.purong.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.purong.purong.Contant;
import com.purong.purong.PermissionConstant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.bean.ImgUpLoadBean;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.net.UpLoadImg;
import com.purong.purong.widget.AvatarPictureDialog;
import com.purong.purong.widget.LoadingDialog;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wenjiezeng on 2017/10/8.
 */
public class UserInfoActivity extends BasePermActivity {
    private AvatarPictureDialog mDialog;
    private RadiusImageView avatar;
    private TextView nick,phone,real;
    private LoadingDialog mloadDialog;


    @Override
    public int getRootViewId() {
        return R.layout.activity_user_info;
    }

    public void initView(){
        showBack();
        showTitle("个人信息");
        avatar=(RadiusImageView) findViewById(R.id.avatar);
        nick=(TextView)findViewById(R.id.nick);
        phone=(TextView)findViewById(R.id.phone);
        real=(TextView)findViewById(R.id.real);
        mloadDialog=new LoadingDialog(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (PuRongApp.getLoginInfo(this)!=null){
            LoginBean bean=PuRongApp.getLoginInfo(this);
            GlideUtil.getInstance().loadAvatar(this,bean.getAvatar(),avatar);
            nick.setText(bean.getNick());
            phone.setText(bean.getPhone());
            if (bean.getIs_real().equals("1")){
                real.setText("未认证");
            }else if (bean.getIs_real().equals("2")){
                real.setText("已认证");
            }
        }else {
            avatar.setImageResource(R.drawable.def_avatar);
            nick.setText("");
            phone.setText("");
            real.setText("");

        }
    }
    private void getUserInfo(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        request(Contant.LOGIN_INFO, params, true,true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOGIN_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    LoginBean bean= JSON.parseObject(responseInfo.content,LoginBean.class);
                    bean.setPwd(PuRongApp.getLoginInfo(this).getPwd());
                    bean.save(this);
                    Intent intent=new Intent(this,RealInfoActivity.class);
                    intent.putExtra("IDENTIFY",bean.getIdentity());
                    intent.putExtra("NAME",bean.getReal());
                    startActivity(intent);
                }catch (Exception e){

                }
            }
        }else if (responseInfo.url.equals(Contant.SET_FIELD)){
            mloadDialog.dismiss();
            if (responseInfo.isRequestOk){
                showToast("头像已上传");
                if (PuRongApp.getLoginInfo(this)!=null){
                    LoginBean bean=PuRongApp.getLoginInfo(this);
                    bean.setAvatar(responseInfo.params.get("web_url"));
                    bean.save(this);
                    GlideUtil.getInstance().loadAvatar(this,bean.getAvatar(),avatar);

                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mDialog != null) {
            mDialog.onActivityResult(requestCode, resultCode, data);
        }
    }
    private void upLoad(String path){
        if (path != null) {
            UpLoadImg upload=new UpLoadImg(UserInfoActivity.this, path,
                    UpLoadImg.UPLOAD_AVATAR, new UpLoadImg.OnUploadImgListener() {
                @Override
                public void onSuccess(String responseString, ImgUpLoadBean bean) {
                    if (bean!=null&&bean.getUrl()!=null){
                            setAvatar(bean.getUrl(),bean.getWeb_url());
                        }
                    mloadDialog.dismiss();
                }

                @Override
                public void onFail(String responseString, Throwable throwable) {
                    mloadDialog.dismiss();
                }
            });
            upload.setNeedScal();
            upload.upLoad();
            mloadDialog.show();
        }
    }
    @Override
    protected void doPermissinosSuc() {
        mDialog = new AvatarPictureDialog(this, new AvatarPictureDialog.OnResultListener() {
            @Override
            public void cameraResult(File file) {
                if (file!=null)
                upLoad(file.getAbsolutePath());

            }

            @Override
            public void albumSelect(List<String> selectPath) {

                if (selectPath != null && selectPath.size() > 0){
                    upLoad(selectPath.get(0));
                }

            }
        });
        mDialog.show();
    }
    @Override
    protected String[] getPermissions() {

        return PermissionConstant.PERMS_PHOTO;
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.avatar_lay:
                if (Build.VERSION.SDK_INT >= 23) {
                    checkPermission();
                } else {
                    mDialog = new AvatarPictureDialog(this, new AvatarPictureDialog.OnResultListener() {
                        @Override
                        public void cameraResult(File file) {
                            if (file!=null)
                                upLoad(file.getAbsolutePath());

                        }

                        @Override
                        public void albumSelect(List<String> selectPath) {
                            if (selectPath != null && selectPath.size() > 0){
                                upLoad(selectPath.get(0));
                            }

                        }
                    });
                    mDialog.show();
                }
                break;
            case R.id.real_lay:
                if (PuRongApp.getLoginInfo(this)!=null){
                    LoginBean bean=PuRongApp.getLoginInfo(this);
                    if (bean.getIs_real().equals("1")){
                        Intent intent=new Intent(this,RealIdentifyActivity.class);
                        startActivity(intent);
                    }else if (bean.getIs_real().equals("2")){
                        getUserInfo();
                    }
                }
                break;
            case R.id.nick_lay:
                Intent intent=new Intent(this,NickEditActivity.class);
                intent.putExtra("NICK",nick.getText());
                startActivity(intent);
                break;
        }
    }
    private void setAvatar(String url,String webUrl){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("field","avatar");
        params.put("value",url);
        params.put("web_url",webUrl);
        if (!request(Contant.SET_FIELD, params,true)){
            mloadDialog.dismiss();
        }
    }
//    class UpLoadImg{
//        private String path;
//        public UpLoadImg(String path){
//            this.path=path;
//        }
//        public void upLoad(){
//            mloadDialog.show();
//            RequestParams params = new RequestParams();
//
//            try {
//                params.put("file", path.endsWith(".gif")||path.endsWith(".GIF")?new File(path): FileUtil.scal(UserInfoActivity.this,path,300));
//                params.put("path","avatar");
//                params.put("_t",SharePreUtil.getInstance(UserInfoActivity.this).getToken());
//
//            } catch (FileNotFoundException e) {
//                mloadDialog.dismiss();
//                e.printStackTrace();
//            }
//            HttpUtil.getInstance().sendPost(Contant.UPLOAD_IMG,params,new ImgHttpResponseHandler(){
//                @Override
//                public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                    super.onSuccess(statusCode, headers, responseString);
//                    Log.e("zwj","...upload success");
//                    responseString=new Encrypt().toDecryption(responseString);
//                    if (responseString==null){
//                        mloadDialog.dismiss();
//                        return;
//                    }
//                    Log.e("zwj","...responseString="+responseString);
//                    try {
//                        ImgUpLoadBean bean=JSON.parseObject(responseString,ImgUpLoadBean.class);
//                        if (bean!=null&&bean.getUrl()!=null){
//                            setAvatar(bean.getUrl(),bean.getWeb_url());
//                        }else {
//                            mloadDialog.show();
//                        }
//                        Log.e("zwj","..url="+bean.getUrl());
//                        Log.e("zwj","..weburl="+bean.getWeb_url());
//                    }catch (Exception e){
//                        mloadDialog.show();
//                    }
//
//                }
//
//                @Override
//                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                    super.onFailure(statusCode, headers, responseString, throwable);
////                    hander.sendEmptyMessage(-2);
//                    mloadDialog.dismiss();
//                    Log.e("zwj","...upload Fail");
//                }
//            });
//        }
//    }
}
